package com.neu.flashmall.ms.interceptor;

import com.neu.flashmall.ms.utils.ManageSystemRedisKeyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

public class UpdateTokenExpireInterceptor implements HandlerInterceptor {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Token");
        if(StringUtils.isEmpty(token)) return true;
        String key = ManageSystemRedisKeyUtil.generateKey(token);
        if(redisTemplate.hasKey(key)){
            redisTemplate.expire(key,1, TimeUnit.DAYS);
        }
        return true;
    }
}
